import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  DefaultValuePipe,
  ParseIntPipe,
  Query,
  SetMetadata
} from '@nestjs/common';
import { RoleService } from './role.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { ApiOperation, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ReflectMetadataKeys } from '@/common/enum/ReflectMetadataKeys';
import { Role } from './entities/role.entity';
import { roleQueryVo } from '@/common/interface';

@ApiTags('角色管理')
@Controller('role')
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @ApiOperation({ summary: '角色创建' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '角色创建')
  @Post()
  create(@Body() createRoleDto: CreateRoleDto) {
    return this.roleService.create(createRoleDto);
  }

  @ApiOperation({ summary: '角色列表' })
  @ApiResponse({ status: 200, description: '200', type: [Role] })
  @ApiQuery({
    name: 'name',
    required: false,
    description: '角色名称',
    type: String,
    example: '管理员'
  })
  @ApiQuery({
    name: 'queryDate',
    required: false,
    description: '查询时间',
    type: Array
  })
  @ApiQuery({
    name: 'page',
    required: false,
    description: '第几页',
    type: Number,
    example: 1
  })
  @ApiQuery({
    name: 'limit',
    required: false,
    description: '每页显示数',
    type: Number,
    example: 10
  })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '角色列表')
  @Get()
  findAll(
    @Query( ) query: roleQueryVo,
    @Query('page', new DefaultValuePipe(1), ParseIntPipe) page = 1,
    @Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit = 10
  ) {
    return this.roleService.findAll({ page, limit }, query);
  }

  @ApiOperation({ summary: '角色详情' })
  @ApiResponse({ status: 200, description: 'success', type: Role })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '角色详情')
  @Get(':id')
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.roleService.findOne(id);
  }

  @ApiOperation({ summary: '修改角色' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '修改角色')
  @Patch()
  update(@Body() updateRoleDto: UpdateRoleDto) {
    return this.roleService.update(updateRoleDto);
  }

  @ApiOperation({ summary: '删除角色' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '删除角色')
  @Delete(':id')
  remove(@Param('id', ParseIntPipe) id: number) {
    return this.roleService.remove(id);
  }

  @ApiOperation({ summary: '禁用/启用角色状态' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '禁用/启用角色状态')
  @Patch('change/:id/:status')
  changeStatus(
    @Param('id', ParseIntPipe) id: number,
    @Param('status', ParseIntPipe) status: number
  ) {
    return this.roleService.changeStatus(id, status);
  }
}
